package com.example.controller;

import com.example.config.AppConfig;
import com.example.config.DatabaseConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;

@RestController
public class ProfileController {

    @Autowired
    private AppConfig appConfig;
    
    @Autowired
    private DatabaseConfig databaseConfig;
    
    @Autowired
    private Environment environment;
    
    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/profile")
    public Map<String, Object> getProfileInfo() {
        Map<String, Object> response = new HashMap<>();
        
        // 应用基本信息
        response.put("appName", appConfig.getName());
        response.put("appDescription", appConfig.getDescription());
        response.put("appEnvironment", appConfig.getEnvironment());
        response.put("appDebug", appConfig.isDebug());
        
        // 服务器信息
        response.put("serverPort", serverPort);
        
        // 数据库配置（在实际应用中，不要直接返回密码）
        Map<String, String> dbInfo = new HashMap<>();
        dbInfo.put("url", databaseConfig.getUrl());
        dbInfo.put("username", databaseConfig.getUsername());
        dbInfo.put("password", "[PROTECTED]"); // 保护密码信息
        response.put("database", dbInfo);
        
        // 当前激活的profile
        response.put("activeProfiles", Arrays.asList(environment.getActiveProfiles()));
        response.put("defaultProfiles", Arrays.asList(environment.getDefaultProfiles()));
        
        return response;
    }
    
    @GetMapping("/hello")
    public String hello() {
        return "Hello from " + appConfig.getEnvironment() + " environment! Running on port " + serverPort;
    }
}